import {Store} from 'iflux2';
import {fromJS} from 'immutable';

import {message} from 'antd';

import DetailActor from './actor/detail-actor';
import ShipCompanyActor from './actor/ship-company-actor';
import RefundActor from './actor/refund-actor';
import {DetailType, CompanyType, RefundType} from './constant/action-type';

import webApi from './webApi';


export default class AppStore extends Store {
    bindActor() {
        return [
            new DetailActor,
            new ShipCompanyActor,
            new RefundActor
        ]
    }


    /**
     * 查询订单详情
     * @param tid
     * @returns {Promise.<void>}
     */
    queryDetail = async (tid)=>{
        try {
            const {orderStatusEnum, tradeOrder, tradeLoggers, logistics, activityRecordInfo, userLogo, ...rest} = await webApi.queryDetail(tid);

            this.dispatch(DetailType.SET_LOGS, fromJS(tradeLoggers));
            this.dispatch(DetailType.SET_LOGISTICS, fromJS(logistics));
            this.dispatch(DetailType.SET_TIME, rest);
            this.dispatch(DetailType.SET_ACTIVITYRECORDINFO, fromJS(activityRecordInfo));
            this.dispatch(DetailType.SET_DETAIL, fromJS(tradeOrder).set('orderStatus', orderStatusEnum));
            this.dispatch(DetailType.SET_USER_LOGOS, userLogo);
            await this.getToken(tradeOrder.buyUsercode);
        } catch(err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    /**
     * 查询物流公司
     * @returns {Promise.<void>}
     */
    queryShipCompanys = async ()=>{
        try {
           const  companys = await webApi.queryShipCompanys();
           this.dispatch(CompanyType.SET_COMPANYS, fromJS(companys))
        } catch(err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    /**
     * 发货操作
     * @param params
     */
    deliveryOrder = async (params)=>{
        try {
            await webApi.deliveryOrder(params);
            message.success('发货成功');
            await this.queryDetail(params.tid);
        } catch (err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    /**
     * 完成订单
     * @param tid
     * @returns {Promise.<*>}
     */
    completeOrder = async (tid)=>{
        try {
            await webApi.completeOrder(tid)
            message.success('操作成功');
            this.queryDetail(tid);
        } catch(err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    /**
     * 取消订单
     * @param tid
     * @returns {Promise.<*>}
     */
    cancelOrder = async (tid)=>{
        try {
            await webApi.cancelOrder(tid);
            message.success('取消订单成功');
            this.queryDetail(tid);
        } catch(err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    setRemark = (remark)=>{
        this.dispatch(DetailType.SET_REMARK, remark);
    }


    editSellerRemark = async (params)=>{
        try {
            await webApi.editSellerRemark(params);
            message.success('修改成功');
            this.queryDetail(params.tid);
        } catch(err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    /**
     * 获取token
     * @param userId
     * @returns {Promise.<void>}
     */
    getToken = async (userId)=>{
        try {
            const {token, adminId} = await webApi.getToken(userId);
            window.cloudshop_adminId = adminId;
            window.cloudshop_jwtToken = token;
            await this.queryUserReceiptList();
        } catch (err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }


    /**
     * 查询收货地址
     * @returns {Promise.<void>}
     */
    queryUserReceiptList = async ()=>{
        try {
            const {dataList} = await webApi.queryUserReceiptList();
            this.dispatch(DetailType.SET_RECEIPTS, fromJS(dataList));
        } catch (err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }

    saveReceipt = async (params)=>{
        try {
            const receiptId = await webApi.saveReceipt(params);
            this.dispatch(DetailType.ADD_RECEIPT, fromJS(Object.assign({}, params, {receiptId})));
            return receiptId;
        } catch (err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }

    modifyBuyerConsignee = async (params)=>{
        try {
            await webApi.modifyBuyerConsignee(params);
            message.success('修改成功');
            this.queryDetail(params.tid);
        } catch (err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }

    refundBefore = async (tid = '')=>{
        try {
            const {applyRefundAmount, payAmount, tradeOrder, payTypeBOList} = await webApi.refundBefore(tid);
            this.dispatch(RefundType.SET_REFUND, Object.assign({applyRefundAmount, payAmount}, {
                tradeOrder: fromJS(tradeOrder),
                payTypeBOList: fromJS(payTypeBOList)
            }))
        } catch (err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }

    refund = async (params)=>{
        try {
            await webApi.refund(params);
            message.success('申请退款成功');
            this.queryDetail(params.tid);
        } catch(err) {
            message.error(err.message);
            return Promise.reject(err);
        }
    }
}